package es.udc.fic.hamNeggs.service;

import es.udc.fic.hamNeggs.manager.MeetingManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.model.DetalleMeeting;
import es.udc.fic.hamNeggs.model.DetalleTarea;
import es.udc.fic.hamNeggs.model.Elemento;
import es.udc.fic.hamNeggs.model.SiNo;
import es.udc.fic.hamNeggs.pojo.Meeting;
import es.udc.fic.hamNeggs.pojo.User;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.Works;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import java.util.ArrayList;
import java.util.List;

@Path("/service")
public class Resource {
    // The @Context annotation allows us to have certain contextual objects
    // injected into this class.
    // UriInfo object allows us to get URI information (no kidding).
    @Context
    UriInfo uriInfo;

    // Another "injected" object. This allows us to use the information that's
    // part of any incoming request.
    // We could, for example, get header information, or the requestor's address.
    @Context
    Request request;

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("existenombreusuario")
    @Produces(MediaType.APPLICATION_JSON)
    public SiNo getSamplePerson(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        return response.getExisteNombreUsuario(usuario);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("listarproyectos")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Elemento> getSampleProyects(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        return response.getListaProyectos(usuario);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("existeusuario")
    @Produces(MediaType.APPLICATION_JSON)
    public SiNo getSampleLogin(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        String password = userParams.getFirst("password");
        return response.getUsuarioValido(usuario, password);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("listarmeetings")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Elemento> getSampleMeeting(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        String proyecto = userParams.getFirst("proyecto");
        return response.getListaMeetings(usuario, proyecto);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("listartareas")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Elemento> getSampleTareas(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        String proyecto = userParams.getFirst("proyecto");
        return response.getListaTareas(usuario, proyecto);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("detallemeeting")
    @Produces(MediaType.APPLICATION_JSON)
    public DetalleMeeting getSampledetalleMeeting(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        String meeting = userParams.getFirst("meeting");
        return response.getMeetingDetail(usuario, meeting);
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("detalletarea")
    @Produces(MediaType.APPLICATION_JSON)
    public DetalleTarea getSampledetalleTarea(MultivaluedMap<String, String> userParams) {
        Response response = new Response();
        String usuario = userParams.getFirst("usuario");
        String tarea = userParams.getFirst("tarea");
        return response.getTareaDetail(usuario, tarea);
    }

    private class Response {
        public SiNo getExisteNombreUsuario(String name) {
            SiNo response = new SiNo();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(name);
            if (user == null)
                response.setRespuesta("No");
            else
                response.setRespuesta("Si");
            usrmng.close();
            return response;
        }

        public SiNo getUsuarioValido(String name, String pass) {
            SiNo response = new SiNo();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(name);
            if (pass.equalsIgnoreCase(user.getPassword()))
                response.setRespuesta("Si");
            else
                response.setRespuesta("No");
            usrmng.close();
            return response;
        }

        public List<Elemento> getListaProyectos(String userName) {
            List<Elemento> response = new ArrayList<Elemento>();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(userName);
            for (Works w : user.getWorks()) {
                Elemento e = new Elemento();
                e.setNombre(w.getProject().getName());
                response.add(e);
            }
            usrmng.close();
            return response;

        }

        public List<Elemento> getListaMeetings(String userName, String projectName) {
            List<Elemento> response = new ArrayList<Elemento>();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(userName);
            for (Works w : user.getWorks()) {
                if (w.getProject().getName().equalsIgnoreCase(projectName)) {
                    for (Meeting m : w.getProject().getMeetings()) {
                        Elemento e = new Elemento();
                        e.setNombre(m.getId() + "-" + m.getDate().toString());
                        response.add(e);
                    }
                }
            }
            usrmng.close();
            return response;
        }

        public List<Elemento> getListaTareas(String userName, String projectName) {
            List<Elemento> response = new ArrayList<Elemento>();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(userName);
            for (Works w : user.getWorks()) {
                if (w.getProject().getName().equalsIgnoreCase(projectName)) {
                    for (UserStories m : w.getProject().getUserStories()) {
                        Elemento e = new Elemento();
                        e.setNombre(m.getName());
                        response.add(e);
                    }
                }
            }
            usrmng.close();
            return response;
        }

        public DetalleMeeting getMeetingDetail(String userName, String meetingName) {
            String[] meeting = meetingName.split("-");
            DetalleMeeting response = new DetalleMeeting();
            final MeetingManager mtgmng = new MeetingManager();
            final Meeting mtg = mtgmng.getMeeting(new Long(meeting[0]));
            if (mtg != null) {
                if (mtg.getId().toString() != null)
                    response.setId(mtg.getId().toString());
                else
                    response.setId("");
                if (mtg.getType().toString() != null)
                    response.setTipo(mtg.getType().toString());
                else
                    response.setTipo("");
                if (mtg.getDate().toString() != null)
                    response.setFecha(mtg.getDate().toString());
                else
                    response.setFecha("");
            }
            mtgmng.close();
            return response;
        }

        public DetalleTarea getTareaDetail(String userName, String tareaName) {
            DetalleTarea response = new DetalleTarea();
            UserManager usrmng = new UserManager();
            User user = usrmng.getUser(userName);
            for (Works w : user.getWorks()) {
                for (UserStories ust : w.getProject().getUserStories()) {
                    if (ust.getName().equalsIgnoreCase(tareaName)) {
                        if (ust.getDescription() != null)
                            response.setDescripcion(ust.getDescription());
                        else
                            response.setDescripcion("");
                        if (ust.getName() != null)
                            response.setNombre(ust.getName());
                        else
                            response.setNombre("");
                        if (ust.getEstimatedstoryPoints() != null)
                            response.setNumpuntos(ust.getEstimatedstoryPoints().toString());
                        else
                            response.setNumpuntos("");
                        if (ust.getRealstoryPoints() != null)
                            response.setNumpuntosreales(ust.getRealstoryPoints().toString());
                        else
                            response.setNumpuntosreales("");
                        if (ust.getState() != null)
                            response.setEstado(ust.getState().getString());
                        else
                            response.setEstado("");
                        if (ust.getUserStoriesCategory() != null)
                            response.setCategory(ust.getUserStoriesCategory().getCategory());
                        else
                            response.setCategory("");
                        if (ust.getSprint() != null)
                            response.setSprint(ust.getSprint().getName());
                        else
                            response.setSprint("");
                    }
                }
            }
            usrmng.close();
            return response;
        }
    }
}
